<?php


namespace app\common;


class Excel
{
    public static function outBase($header = "", $data = "", $title = "")
    {
//        $header = ['邮箱', '昵称', '节数', '套餐', '金额', '币种', '支付渠道', '支付状态', '下单时间'];
//        $data=[
//            ['123@123.com','123','12','10节课程套餐','129','美元','PayPal','已支付','2020-80-03 07:12:45'],
//            ['123@123.com','123','12','10节课程套餐','129','美元','PayPal','已支付','2020-80-03 07:12:45'],
//            ['123@123.com','123','12','10节课程套餐','129','美元','PayPal','已支付','2020-80-03 07:12:45'],
//            ['123@123.com','123','12','10节课程套餐','129','美元','PayPal','已支付','2020-80-03 07:12:45'],
//            ['123@123.com','123','12','10节课程套餐','129','美元','PayPal','已支付','2020-80-03 07:12:45'],
//            ['123@123.com','123','12','10节课程套餐','129','美元','PayPal','已支付','2020-80-03 07:12:45'],
//            ['123@123.com','123','12','10节课程套餐','129','美元','PayPal','已支付','2020-80-03 07:12:45'],
//            ['123@123.com','123','12','10节课程套餐','129','美元','PayPal','已支付','2020-80-03 07:12:45'],
//        ];
        $cols=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'];
        require_once "./extend/phpexcel/Classes/PHPExcel.php";
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
            ->setLastModifiedBy("Maarten Balliauw")
            ->setTitle("PHPExcel Test Document")
            ->setSubject("PHPExcel Test Document")
            ->setDescription("Test document for PHPExcel, generated using PHP classes.")
            ->setKeywords("office PHPExcel php")
            ->setCategory("Test result file");

        //处理数据
        //表头
        foreach ($header as $k => $v) {
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue($cols[$k]."1", $v);
        }
        //数据
        $j=2;
        foreach($data as $key => $value) {
            foreach($value as $kk=>$vv){
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue($cols[$kk].$j, $vv);
            }
            $j++;

        }
        ob_end_clean();
        ob_start();
        $objPHPExcel->getActiveSheet()->setTitle('订单数据');
        //导出下载表格
        $filename=$title?$title:date('YmdHis',time());
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$filename.".xlsx".'"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate');
        header ('Pragma: public');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
    }

}